﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace VNet.VisiSuite.Service.Contact.Message.ProductionData
{
    [DataContract(Namespace = VisiSuiteNamespace.Uri + "dto/AxisDetailItem")]
    public class AxisDetailItem : System.ComponentModel.INotifyPropertyChanged
    {
        private int? _target;
        private int? _good;
        private int? _defect;
        private string _color;
        private string _label;
        private DateTime _start_time;
        private DateTime _end_time;
        private int _index;

        public AxisDetailItem(DateTime start, DateTime end)
        {
            Target = null;
            Good = null;
            Defect = null;
            Color = null;
            StartTime = start;
            EndTime = end;
        }

        [DataMember(IsRequired = true)]
        public int? Target
        {
            get { return _target; }
            set
            {
                _target = value;
                OnPropertyChanged("Target");
            }
        }

        [DataMember(IsRequired = true)]
        public int? Good
        {
            get { return _good; }
            set
            {
                _good = value;
                OnPropertyChanged("Good");
            }
        }

        [DataMember(IsRequired = true)]
        public int? Defect
        {
            get { return _defect; }
            set
            {
                _defect = value;
                OnPropertyChanged("Defect");
            }
        }

        [DataMember(IsRequired = true)]
        public string Color
        {
            get { return _color; }
            set
            {
                _color = value;
                OnPropertyChanged("Color");
            }
        }

        [DataMember(IsRequired = true)]
        public string Label
        {
            get { return _label; }
            set
            {
                _label = value;
                OnPropertyChanged("Label");
            }
        }

        [DataMember(IsRequired = true)]
        public DateTime StartTime
        {
            get { return _start_time; }
            set
            {
                _start_time = value;
                Label = formatHour(_start_time.Hour, "");
                OnPropertyChanged("StartTime");
            }
        }

        [DataMember(IsRequired = true)]
        public DateTime EndTime
        {
            get { return _end_time; }
            set
            {
                _end_time = value;
                OnPropertyChanged("EndTime");
            }
        }

        // Just for binding
        [DataMember(IsRequired = true)]
        public int Index
        {
            get { return _index; }
            set
            {
                _index = value;
                OnPropertyChanged("Index");
            }
        }

        static public string formatHour(int hour, string template)
        {
            string fmt = string.Empty;
            if (hour == 0 || hour == 24)
                fmt = "12 AM";
            else if (hour < 12)
                fmt = hour.ToString(template) + " AM";
            else if (hour == 12)
                fmt = "12 PM";
            else if (hour < 24)
                fmt = (hour - 12).ToString(template) + " PM";
            return fmt;
        }

        #region INotifyPropertyChanged
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}
